package ninja.awesome.game.gameobject;

import java.util.Random;

import ninja.awesome.framework.Graphics;
import ninja.awesome.framework.Image;
import ninja.awesome.framework.Sound;
import ninja.awesome.game.Assets;
import ninja.awesome.game.MainGameScreen;
import ninja.awesome.game.NinjaGame;
import ninja.awesome.game.classes.Hostile;
import ninja.awesome.game.compat.Rectangle;
import ninja.awesome.game.main.Animation;
import ninja.awesome.game.main.Controller;
import ninja.awesome.game.main.Physics;
import ninja.awesome.game.main.Sprites;
import android.graphics.Color;

public class Boss extends GameObj implements Hostile {

	private int HEALTH1 = 150;
	private int HEALTH2 = 150;
	private boolean alternativeHealth = false;
	private boolean isDead = false;
	private Image image;
	private Image[] e;
	private int bulletSpeed = 11;
	private int speed = 11;// I changed for jump down speed
	// private int xSpeed = 2;
	private int bossAction = 0;
	private int attackSpeed = 0; // Jung added the more datafield
	private int ticks = 0;
	private int timer = 0;
	private double xpAmountDropped = 100;
	private boolean isJumpDown = false;
	private int levelOfStage = 0; // Boss suppose to be changed depend on each
									// level.

	// added for new data field for boss.
	private int imageNumber = 1;
	private double imageWidth = 0.0;
	private double imageHeight = 0.0;

	private String direction = "jumpdown"; // I make a boss to move aroud
											// easier!
											// New Source Code Attention!
	Animation explosion, anim;
	Random r = new Random();
	Sound boom;
	MainGameScreen game;
	Sprites s;
	Controller c;

	public Boss(int x, int y, MainGameScreen game, Controller c, Sprites s) {
		super(x, y);
		this.s = s;
		this.c = c;
		this.game = game;
		image = s.getBoss();
		e = s.getExplosion2();
		boom = Assets.boom;
		explosion = new Animation(5, image, imageWidth, imageHeight, imageNumber);
	}

	@Override
	public void update() {
		// if (isDead) {
		// explosion.runAnimation();
		// if (timer == 40) {
		// game.setGameOver(true);
		// // game.setWin(true);
		// setTimer(0);
		// // 1P
		// if (game.getPlayingMode() == PlayingMode.SINGLE) {
		// game.getNinja().setScore(game.getNinja().getScore());
		// game.getNinja().setExperiencePoint(game.getNinja().getExperiencePoint()
		// + xpAmountDropped);
		// }
		// // 2P
		// if (game.getPlayingMode() == PlayingMode.MULTIPLE) {
		// game.getNinja().setScore(game.getNinja().getScore() + 150);
		// game.getNinja2().setScore(game.getNinja2().getScore() + 150);
		// }
		// c.removeHostile(this);
		// }
		// timer++;
		// }
		/*
		 * The boss movement is here and action!
		 * ********************************
		 * **************************************
		 * *****************************************
		 */

		if (isDead) {
			c.getBossList().remove(this);
		} else {
			int whenAttack = r.nextInt(61) + 100;
			if (isJumpDown)
				if (ticks % whenAttack == 0) {
					this.setDirection("left");
					this.setSpeed(0);
				}
			if (direction.equals("jumpdown")) {
				yLoc += speed;// y
				if (yLoc >= 450) {
					isJumpDown = true;
					this.setDirection("up");
					isJumpDown = true;

				}
			} else if (direction.equals("up")) {
				this.setSpeed(-3);
				if (yLoc <= 50)
					this.setDirection("down");

			} else if (direction.equals("down")) {
				// System.out.println("Boss " + direction + "\n"); // for degug
				// purpose;
				this.setSpeed(3);
				if (yLoc >= 450) {// x
					this.setDirection("up");

				}
			} else if (direction.equals("left")) {
				this.setAttackSpeed(-10);
				if (xLoc <= 10) {
					this.setDirection("right");
					this.setAttackSpeed(0);
				}
			} else if (direction.equals("right")) {
				this.setAttackSpeed(10);
				if (xLoc >= NinjaGame.WIDTH - 200) {
					this.setDirection("up");
					this.setAttackSpeed(0);
					bossAction = 0;
				}

			}

			/**********************************************************************************************************************/
			yLoc += this.getSpeed();
			xLoc += this.getAttackSpeed() - game.getNinja().getSpeedX();
			// SHOOTING AI
			if (ticks % 40 == 0) { // this might be helpful for delay!
				if (r.nextInt(2) == 1) {
					c.addHostile(new BossAttack(this.xLoc - 40, this.yLoc + 50, this.bulletSpeed + 1, this.game, this.c, this.s));
					c.addHostile(new BossAttack(this.xLoc - 20, this.yLoc + 50, this.bulletSpeed + 1, this.game, this.c, this.s));
					c.addHostile(new BossAttack(this.xLoc + 20, this.yLoc + 50, this.bulletSpeed + 1, this.game, this.c, this.s));
					c.addHostile(new BossAttack(this.xLoc + 40, this.yLoc + 50, this.bulletSpeed + 1, this.game, this.c, this.s));
					if (r.nextInt(3) == 1) {
						// c.addHostile(new StrongBullet(this.xLoc, this.yLoc,
						// this.speed + 1, this.game, this.c, this.s));
					}
				}
			}

			// COLLISSION WITH BULLETS
			for (int i = 0; i < game.fl.size(); i++) {
				Attack tempFriend = (Attack) game.fl.get(i);
				if (Physics.collision(this, tempFriend)) {
					// 1P
					if (!tempFriend.getHit()) {
						if (!alternativeHealth) {
							setHEALTH1(this.getHEALTH1() - 5);
						} else
							setHEALTH2(this.getHEALTH2() - 5);
					}

					// 2P

					tempFriend.setHit(true);
					if (getHEALTH1() <= 0)
						alternativeHealth = true;
					if (getHEALTH1() <= 0 && getHEALTH2() <= 0) {
						this.setIsDead(true);
					}
					if (tempFriend.getTimer() == 0) {
						boom.play(1f);
					}
				}
			}
		}

		ticks++;
	}

	@Override
	public void draw(Graphics g) {
		if (!isDead) {
			g.fillRect(xLoc, yLoc - 20, 150, 20, Color.RED);

			if (getHEALTH1() >= 0 && getHEALTH2() >= 0) {
				g.fillRect(xLoc, yLoc - 20, this.getHEALTH2(), 20, Color.YELLOW);
				g.fillRect(xLoc, yLoc - 20, this.getHEALTH1(), 20, Color.GREEN);
			}

			g.drawRect(xLoc, yLoc - 20, 150, 20, Color.GRAY);

			g.drawImage(image, xLoc, yLoc - 20);
		} else if (isDead) {
			// explosion.drawAnimation(g, xLoc + 20, yLoc, 0);
			// explosion.drawAnimation(g, xLoc + 52, yLoc, 0);
			// explosion.drawAnimation(g, xLoc + 84, yLoc, 0);
			// explosion.drawAnimation(g, xLoc + 30, yLoc + 40, 0);
			// explosion.drawAnimation(g, xLoc + 72, yLoc + 40, 0);
		}
	}

	@Override
	public Rectangle getBounds() {
		return new Rectangle(xLoc, yLoc - 10, 160, 90);
	}

	@Override
	public int getX() {
		return xLoc;
	}

	@Override
	public int getY() {
		return yLoc;
	}

	@Override
	public void setIsDead(boolean n) {
		isDead = n;
	}

	@Override
	public boolean getIsDead() {
		return isDead;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public void setAttackSpeed(int attackSpeed) {
		this.attackSpeed = attackSpeed;
	}

	public int getAttackSpeed() {
		return attackSpeed;
	}

	public int getHEALTH1() {
		return HEALTH1;
	}

	public int getHEALTH2() {
		return HEALTH2;
	}

	public void setHEALTH1(int HEALTH) {
		this.HEALTH1 = HEALTH;
	}

	public void setHEALTH2(int HEALTH) {
		this.HEALTH2 = HEALTH;
	}

	public void setDead(boolean isDead) {
		this.isDead = isDead;
	}

	public void setTimer(int n) {
		timer = n;
	}

	public void setDirection(String direction) {
		this.direction = direction;
	}

	public String getDirection() {
		return this.direction;
	}
}